import os
from subprocess import  Popen, PIPE
from Tkinter import *
import tkSimpleDialog
def _svn_checkout(url):
	return _run_program('svn/svn.exe', 'co', url, "resources/src/")
def _svn_update_irgts():
	return _run_program('svn/svn.exe', 'update', "resources/src/")
def _svn_update_sendpkm():
	return _run_program('svn/svn.exe', 'update', "customresources/src/sendpkmwrapper.py")

def _run_program(*args):
	popen_obj = Popen(args, stderr=PIPE)
	_, errors = popen_obj.communicate()
	if popen_obj.returncode:
		print errors

def isFirstUpdate(gamegen):
	gamegen += 3
	if gamegen == 4:
		_svn_checkout("http://ir-gts.googlecode.com/svn/ir-gts/src/")
	elif gamegen == 5:
		_svn_checkout("http://ir-gts.googlecode.com/svn/ir-gts-bw/src/")

def updateResources():
	if os.path.exists("resources/src/sendpkm.py"):
		_svn_update_irgts()
		_svn_update_sendpkm()
		return
	else:
		root = Tk()
		root.withdraw()
		isFirstUpdate(tkSimpleDialog.askinteger("Generation Select", "1 - D/P/Pl/HG/SS\n2 - B/W",minvalue=1,maxvalue=2,parent=root))
		root.destroy()
		return
